define(['moment', 'module/moduleMobile/program/mobile05/Rollback.service'], function (moment) {
    return ['$scope', '$rootScope', '$mdDialog', 'MMWService', '$filter', '$stateParams', 'config', '$RollbackService', '$document',
        function ($scope, $rootScope, $mdDialog, $MMWService, $filter, $stateParams, config, $RollbackService, $document) {
            $scope.init = function(){
                $scope.status_list = [{name:'Run', value:'1'}, {name:'Queue', value: '0'}, {name:"INV", value: '11'}];
                $scope.rollbackInfo = {};
                $scope.plot_filter = '';
            };

            $scope.back = function(){
                $rootScope.changeProgram($stateParams.data.parent, {data: {}}, true);
            };

            $scope.QRCodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        $scope.barcode_plot_no = result.text;
                        input_plot_no_callback();
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('common.msg.cant_use_camera'));
                    },
                    orientation : 'portrait'
                });
            };

            var element_plot_no = document.getElementById('input_plot');
            //barcode輸入
            $scope.input_plot_no = function(event){
                if(event.keyCode == 13){
                    element_plot_no.blur();
                    input_plot_no_callback();
                } else {
                    $scope.barcode_plot_no = $scope.barcode_plot_no||'';
                }
            };

            $scope.changeTab = function(tab_index){
                $scope.status_list.forEach(function(data, index){
                    if(index == tab_index){
                        data.select = true;
                    } else {
                        data.select = false;
                    }
                });
            };
           
            $scope.getTabSize = function(status){
                var plot_detail = $scope.rollbackInfo.plot_detail || [];

                if(plot_detail.length > 0){
                    plot_detail = $filter('filter')(plot_detail, {status: status}, true);
                    plot_detail = $filter('filter')(plot_detail, $scope.plotFilter);
                }

                return plot_detail.length;
            };

            $scope.plotFilter = function(item){
                if(item.op_no.indexOf($scope.plot_filter) > -1 ){
                    return true;
                } else if(item.op_name.indexOf($scope.plot_filter) > -1){
                    return true;
                } else if(item.machine_no.indexOf($scope.plot_filter) > -1){
                    return true;
                } else if(item.machine_name.indexOf($scope.plot_filter) > -1){
                    return true;
                }
            };

            $scope.rollback = function(item){
                $RollbackService.init({
                    plot_no: item.plot_no,
                    op_no: item.op_no,
                    machine_no: item.machine_no,
                    status: item.status,
                    callback: function(){
                        var barcode_plot_no = $scope.rollbackInfo.plot_no;
                        rollbackPlotInfoGet(barcode_plot_no, function(data){
                            $scope.rollbackInfo = data;
                            $scope.rollbackInfo.plot_no = barcode_plot_no;
                        });
                    }
                });
            };

            function input_plot_no_callback(){
                //檢查輸入的對不對
                var barcode_plot_no = $scope.barcode_plot_no;
                $scope.barcode_plot_no = '';
                if(barcode_plot_no && barcode_plot_no.length > 0){
                    rollbackPlotInfoGet(barcode_plot_no, function(data){
                        $scope.rollbackInfo = data;
                        $scope.rollbackInfo.plot_no = barcode_plot_no;
                    });
                } else {
                    $scope.$root.showAlert($filter('translate')('mobile05.msg.plot_no_empty'));
                    element_plot_no.focus();
                }
            }

            function rollbackPlotInfoGet(plot_no, callback){
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_WIP.rollback_plot_info_get',
                    content: {
                        plot_no: plot_no
                    },
                    success: function (data) {
                        if (callback)
                            callback(data);
                    }
                });
            }   
        }
    ];
});
